var NotificationCenter = require('../tools/SimpleNotificationCenter');
cc.Class({
    extends: cc.Component,

    properties: {
        storeItem: {
            default: null,
            type: cc.Prefab
        }
    },

    // use this for initialization
    onLoad: function () {
        //初始化种子
        this.initSeeds();
        //设置事件
        this.setEvents();
    },
    setEvents: function () {
        this.node.getChildByName('CloseBtn').on('touchend', function (event) {
            cc.log("close");

            //通知关闭植物Store
            NotificationCenter.notify("hideSeedStore", this);
            NotificationCenter.notify("pickedSeed", this, {
                isClose: true,
                model: null
            });

            //阻止冒泡
            event.stopPropagation();
        }, this);
    },

    //初始化种子
    initSeeds: function () {
        //种子数据源
        var seedSourceUrl = cc.url.raw("resources/datas/seed.json");

        //容器
        var content = this.node.getChildByName('Store').getChildByName('Content');

        cc.loader.load(seedSourceUrl, function (err, data) {
            if (err) {
                cc.log(err);
                return;
            }

            var itemBaseUrl = "textures/icons.plist/";
            //解析种子
            for (var name in data) {
                //获取数据data
                var seedItemData = data[name];

                //创建Item
                var seedItem = cc.instantiate(this.storeItem);

                //设置Item属性
                var seedItemObject = seedItem.getComponent('StoreItem');
                seedItemObject.itemName = seedItemData.name;
                seedItemObject.price = seedItemData.price;
                seedItemObject.textureUrl = itemBaseUrl + name;
                seedItemObject.model = seedItemData;
                //添加到当前容器下
                content.addChild(seedItem);
            }
        }.bind(this));
    }

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
